<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言之前项目都是用的vue2.0，目前打算自己看看react，想用react自己实现一个简单的项目吧，因为用过其他框架，也不算是零基础吧。但可能本篇的内容也算是讲下自己初学react时遇到的坑吧，内容比较简单，最后打算自己用react封装一个todoList的组件就算完成了react第一步的学习吧，之后再开始理解并运用react hooks。 基本知识setState使用注意1、不要直接修改 st">
<meta property="og:type" content="article">
<meta property="og:title" content="react初学">
<meta property="og:url" content="http://example.com/2021/05/10/react/react%E5%88%9D%E5%AD%A6/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="前言之前项目都是用的vue2.0，目前打算自己看看react，想用react自己实现一个简单的项目吧，因为用过其他框架，也不算是零基础吧。但可能本篇的内容也算是讲下自己初学react时遇到的坑吧，内容比较简单，最后打算自己用react封装一个todoList的组件就算完成了react第一步的学习吧，之后再开始理解并运用react hooks。 基本知识setState使用注意1、不要直接修改 st">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-05-10T04:18:39.000Z">
<meta property="article:modified_time" content="2021-11-03T08:20:56.340Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="react初学">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/05/10/react/react%E5%88%9D%E5%AD%A6/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>react初学 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/05/10/react/react%E5%88%9D%E5%AD%A6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          react初学
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-05-10 12:18:39" itemprop="dateCreated datePublished" datetime="2021-05-10T12:18:39+08:00">2021-05-10</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-03 16:20:56" itemprop="dateModified" datetime="2021-11-03T16:20:56+08:00">2021-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/react/" itemprop="url" rel="index"><span itemprop="name">react</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>之前项目都是用的vue2.0，目前打算自己看看react，想用react自己实现一个简单的项目吧，因为用过其他框架，也不算是零基础吧。但可能本篇的内容也算是讲下自己初学react时遇到的坑吧，内容比较简单，最后打算自己用react封装一个todoList的组件就算完成了react第一步的学习吧，之后再开始理解并运用react hooks。</p>
<h1 id="基本知识"><a href="#基本知识" class="headerlink" title="基本知识"></a>基本知识</h1><h2 id="setState"><a href="#setState" class="headerlink" title="setState"></a>setState</h2><h3 id="使用注意"><a href="#使用注意" class="headerlink" title="使用注意"></a>使用注意</h3><p><strong>1、不要直接修改 state(状态)。</strong>因为和vue的双向绑定不同，这样是不会触发界面的重新渲染的，一定要使用this.setState()；需要注意的是唯一可以分配 this.state 的地方是构造函数。</p>
<p><strong>2、state(状态) 更新可能是异步的。</strong>React 为了优化性能，有可能会将多个 setState() 调用合并为一次更新。因为this.props和this.state 可能是异步更新的，你不能依赖他们的值计算下一个state(状态)。我们并不能通过上述代码得到想要的值，为了弥补这个问题，使用另一种 setState() 的形式，接受一个函数。这个函数将接收前一个状态作为第一个参数，应用更新时的 props 作为第二个参数，代码如下</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span>.setState(&#123;</span><br><span class="line">  counter: <span class="keyword">this</span>.state.counter + <span class="keyword">this</span>.props.increment,</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">this</span>.setState(<span class="function">(<span class="params">prevState, props</span>) =&gt;</span> (&#123;</span><br><span class="line">  counter: prevState.counter + props.increment</span><br><span class="line">&#125;));</span><br></pre></td></tr></table></figure>

<p><strong>3.state(状态)更新会被合并。</strong>当你调用 setState()， React 将合并你提供的对象到当前的状态中。所以当State是一个多键值的结构时，可以单独更新其中的一个，此时会进行“差分”更新，不会影响其他的属性值。</p>
<h3 id="props-与-state-的区别"><a href="#props-与-state-的区别" class="headerlink" title="props 与 state 的区别"></a>props 与 state 的区别</h3><p>state 的主要作用是用于组件保存、控制、修改自己的可变状态，在组件内部进行初始化，也可以在组件内部进行修改，但是组件外部不能修改组件的 state。</p>
<p>props 的主要作用是让使用该组件的父组件可以传入参数来配置该组件，它是外部传进来的配置参数，组件内部无法控制也无法修改<br>state 和 props 都可以决定组件的外观和显示状态。通常，props 做为不变数据或者初始化数据传递给组件，可变状态使用 state</p>
<h3 id="setState-的异步更新"><a href="#setState-的异步更新" class="headerlink" title="setState()的异步更新"></a>setState()的异步更新</h3><p><strong>1.执行setState()之后干了什么？</strong></p>
<p>setState()方法通过一个队列机制实现state更新，当执行setState()的时候，会将需要更新的state合并之后放入状态队列，而不会立即更新this.state(可以和浏览器的事件队列类比)。如果我们不使用setState而是使用this.state.key来修改，将不会触发组件的re-render。如果将this.state赋值给一个新的对象引用，那么其他不在对象上的state将不会被放入状态队列中，当下次调用setState()并对状态队列进行合并时，直接造成了state丢失。</p>
<p><strong>2.setState()可以接受一个函数作为参数？</strong></p>
<p>setState() 不仅能够接受一个对象作为参数，还能够接受一个函数作为参数。函数的参数即为 state 的前一个状态以及 props。</p>
<p>React文档中对setState的说明如下：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void setState (function|object nextState,[function callback])</span><br></pre></td></tr></table></figure>

<p>上述代码的第二个参数是一个回调函数，在setState() 的异步操作结束并且组件已经重新渲染的时候执行。换句话说，我们可以通过这个回调来拿到更新的state的值。</p>
<p><strong>3.执行setState()后能拿到最新的state值吗？</strong></p>
<p>以前在写代码时候，总是遇到明明执行过setState()，但是state的值却不是最新的，那么如何解决这个问题呢？</p>
<p>因为setState()函数接受两个参数，一个是一个对象，就是设置的状态，还有一个是一个回调函数，是在设置状态成功之后执行的，所以我们可以通过回调拿到最新的state值。代码如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">updateData = <span class="function">(<span class="params">newData</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">this</span>.setState(</span><br><span class="line">        &#123; <span class="attr">data</span>: newData &#125;,</span><br><span class="line">        () =&gt; &#123;</span><br><span class="line">            <span class="comment">//这里打印的是最新的state值</span></span><br><span class="line">            <span class="built_in">console</span>.log(that.state.data);</span><br><span class="line">        &#125;</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>4.setState()一定是异步更新吗？</strong> </p>
<p>我们先来看看下面的代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">incrementMultiple</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">this</span>.setState(&#123;<span class="attr">count</span>: <span class="keyword">this</span>.state.count + <span class="number">1</span>&#125;);</span><br><span class="line">      <span class="keyword">this</span>.setState(&#123;<span class="attr">count</span>: <span class="keyword">this</span>.state.count + <span class="number">1</span>&#125;);</span><br><span class="line">      <span class="keyword">this</span>.setState(&#123;<span class="attr">count</span>: <span class="keyword">this</span>.state.count + <span class="number">1</span>&#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>直观上来看，当上面的 incrementMultiple 函数被调用时，组件状态的 count 值被增加了3次，每次增加1，那最后 count 被增加了3。但是，实际上的结果只给 state 增加了1。</p>
<p>事实上，setState 方法与包含在其中的执行是一个很复杂的过程，从 React 最初的版本到现在，也有无数次的修改。它的工作除了要更动 this.state 之外，还要负责触发重新渲染，这里面要经过 React 核心 diff 算法，最终才能决定是否要进行重渲染，以及如何渲染。而且为了批次与效能的理由，多个 setState 呼叫有可能在执行过程中还需要被合并，所以它被设计以延时的来进行执行是相当合理的。</p>
<p>在 React 的 setState 函数实现中，会根据一个变量 isBatchingUpdates 判断是直接更新 this.state 还是放到队列中回头再说，而 isBatchingUpdates 默认是 false，也就表示 setState 会同步更新 this.state，但是，有一个函数 batchedUpdates，这个函数会把 isBatchingUpdates 修改为 true，而当 React 在调用事件处理函数之前就会调用这个 batchedUpdates，造成的后果，就是由 React 控制的事件处理过程 setState 不会同步更新 this.state。</p>
<p>也就是说，在 React 控制之外的情况， setState 会同步更新 this.state！但大部份的使用情况下，我们都是使用了 React 库中的表单组件，例如 select、input、button 等等，它们都是 React 库中人造的组件与事件，是处于 React 库的控制之下，比如组件原色 onClick 都是经过 React 包装。在这个情况下，setState 就会以异步的方式执行。</p>
<h2 id="事件处理"><a href="#事件处理" class="headerlink" title="事件处理"></a>事件处理</h2><p>1、使用 JSX 语法时你需要传入一个函数作为事件处理函数，而不是一个字符串。</p>
<p>2、在 React 中另一个不同点是你不能通过返回 <code>false</code> 的方式阻止默认行为。你必须显式的使用 <code>preventDefault</code> 。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;activateLasers&#125;</span>&gt;</span></span><br><span class="line">  Activate Lasers</span><br><span class="line"><span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>3、你必须谨慎对待 JSX 回调函数中的 <code>this</code>，在 JavaScript 中，class 的方法默认不会<a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind" target="_blank" rel="noopener">绑定</a> <code>this</code>。如果你忘记绑定 <code>this.handleClick</code> 并把它传入了 <code>onClick</code>，当你调用这个函数的时候 <code>this</code> 的值为 <code>undefined</code>。</p>
<p>4、在循环中，通常我们会为事件处理函数传递额外的参数。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;(e)</span> =&gt;</span> this.deleteRow(id, e)&#125;&gt;Delete Row<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;this.deleteRow.bind(this,</span> <span class="attr">id</span>)&#125;&gt;</span>Delete Row<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h1 id="ToDoList实践"><a href="#ToDoList实践" class="headerlink" title="ToDoList实践"></a>ToDoList实践</h1><p>该组件提供的主要功能就是，用户可以在一个输入框中输入toDo的事项，然后可以在后面的列表中进行显示，同时对于已经完成的事项，可以进行不同的渲染显示。根据数据驱动视图的逻辑，构建步骤如下：1、确定数据格式；2、根据数据完成列表的渲染；3、完成添加功能；4、完成单项错误功能。</p>
<h2 id="APP-js"><a href="#APP-js" class="headerlink" title="APP.js"></a>APP.js</h2><p>我们先实现数据视图的渲染，之后再新建方法来进行各种操作方法的定义与使用。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React,&#123;PureComponent&#125; <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> Title <span class="keyword">from</span> <span class="string">'./title'</span>;</span><br><span class="line"><span class="keyword">import</span> Create <span class="keyword">from</span> <span class="string">'./create'</span>;</span><br><span class="line"><span class="keyword">import</span> Todos <span class="keyword">from</span> <span class="string">'./todos'</span>;</span><br><span class="line"><span class="keyword">import</span> State <span class="keyword">from</span> <span class="string">'./state'</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">"./index.css"</span>;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">  数据驱动视图</span></span><br><span class="line"><span class="comment">   1. 确定数据格式</span></span><br><span class="line"><span class="comment">   2. 根据数据完成列表的渲染</span></span><br><span class="line"><span class="comment">   3. 完成添加功能</span></span><br><span class="line"><span class="comment">   4. 完成单项错误</span></span><br><span class="line"><span class="comment">   data:[</span></span><br><span class="line"><span class="comment">      &#123;</span></span><br><span class="line"><span class="comment">        id: 0,</span></span><br><span class="line"><span class="comment">        txt: "数据内容",</span></span><br><span class="line"><span class="comment">        done: false</span></span><br><span class="line"><span class="comment">      &#125;</span></span><br><span class="line"><span class="comment">   ]</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">App</span> <span class="keyword">extends</span> <span class="title">PureComponent</span> </span>&#123;</span><br><span class="line">  state = &#123;</span><br><span class="line">      data:[</span><br><span class="line">          &#123;</span><br><span class="line">            id: <span class="number">0</span>,</span><br><span class="line">            txt: <span class="string">"数据内容"</span>,</span><br><span class="line">            done: <span class="literal">false</span></span><br><span class="line">          &#125;</span><br><span class="line">      ]</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 添加数据</span></span><br><span class="line">  addData=<span class="function">(<span class="params">txt</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;data&#125; = <span class="keyword">this</span>.state;</span><br><span class="line">    data.unshift(&#123;</span><br><span class="line">      id: <span class="built_in">Date</span>.now(),<span class="comment">//利用获取当前的时间，来确保不重名，id用symbol的话就没办法后序对比</span></span><br><span class="line">      txt,</span><br><span class="line">      done: <span class="literal">false</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">this</span>.setState(&#123;</span><br><span class="line">      data:[...data]</span><br><span class="line"><span class="comment">//pureComponent做的是浅对比，当直接用data时，发现路径没有改变，就不会有视图的更新；因此一定要返回新对象</span></span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 修改完成状态</span></span><br><span class="line">  changeDone=<span class="function">(<span class="params">id,done</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;data&#125; = <span class="keyword">this</span>.state;</span><br><span class="line">    data.forEach(<span class="function"><span class="params">item</span>=&gt;</span>&#123;</span><br><span class="line">      <span class="keyword">if</span>(item.id === id)&#123;</span><br><span class="line">        item.done = done;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">this</span>.setState(&#123;</span><br><span class="line">      data:data.map(<span class="function"><span class="params">item</span>=&gt;</span>(&#123;...item&#125;))</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 修改事项信息</span></span><br><span class="line">  editTxt=<span class="function">(<span class="params">id,txt</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;data&#125; = <span class="keyword">this</span>.state;</span><br><span class="line">    <span class="built_in">console</span>.log(id,txt);</span><br><span class="line">    data.forEach(<span class="function"><span class="params">item</span>=&gt;</span>&#123;</span><br><span class="line">      <span class="keyword">if</span>(item.id === id)&#123;</span><br><span class="line">        item.txt = txt;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">this</span>.setState(&#123;</span><br><span class="line">      data:data.map(<span class="function"><span class="params">item</span>=&gt;</span>(&#123;...item&#125;))</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 删除单项</span></span><br><span class="line">  remove=<span class="function">(<span class="params">id</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;data&#125; = <span class="keyword">this</span>.state;</span><br><span class="line">    data = data.filter(<span class="function"><span class="params">item</span>=&gt;</span>item.id!==id);</span><br><span class="line">    <span class="keyword">this</span>.setState(&#123;</span><br><span class="line">      data</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//删除已完成的</span></span><br><span class="line">  removeDone=<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;data&#125; = <span class="keyword">this</span>.state;</span><br><span class="line">    data = data.filter(<span class="function"><span class="params">item</span>=&gt;</span>!item.done);</span><br><span class="line">    <span class="keyword">this</span>.setState(&#123;</span><br><span class="line">      data</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  render()&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;data&#125; = <span class="keyword">this</span>.state; </span><br><span class="line">    <span class="keyword">return</span> (<span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"todoapp"</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">Title</span> /&gt;</span></span></span><br><span class="line">        &lt;div className="content"&gt;</span><br><span class="line">            &lt;Create addData=&#123;this.addData&#125; /&gt;    </span><br><span class="line">            &#123;data.length&lt;1?"":[</span><br><span class="line">                &lt;Todos</span><br><span class="line">                    key=&#123;1&#125;</span><br><span class="line">                    data=&#123;data&#125; </span><br><span class="line">                    changeDone=&#123;this.changeDone&#125; </span><br><span class="line">                    editTxt=&#123;this.editTxt&#125;</span><br><span class="line">                    remove = &#123;this.remove&#125;</span><br><span class="line">                /&gt;,</span><br><span class="line">                &lt;State </span><br><span class="line">                  data=&#123;data&#125; </span><br><span class="line">                  key=&#123;2&#125;</span><br><span class="line">                  removeDone=&#123;this.removeDone&#125;</span><br><span class="line">                /&gt;</span><br><span class="line">            ]&#125;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span>)</span></span><br><span class="line"><span class="xml">  &#125;</span></span><br><span class="line"><span class="xml">&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="xml">export default App;</span></span><br></pre></td></tr></table></figure>

<h2 id="create-js"><a href="#create-js" class="headerlink" title="create.js"></a>create.js</h2><p>Create是视图中输入框的控件，用户使用其来对todolist列表增加项目数。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React,&#123;PureComponent&#125; <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Create</span> <span class="keyword">extends</span> <span class="title">PureComponent</span> </span>&#123;</span><br><span class="line">  state=&#123;</span><br><span class="line">    val:<span class="string">""</span></span><br><span class="line">  &#125;</span><br><span class="line">  render()&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;addData&#125; = <span class="keyword">this</span>.props;</span><br><span class="line">    <span class="keyword">return</span> (<span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"create-todo"</span>&gt;</span></span></span><br><span class="line">        &lt;input </span><br><span class="line">          id="new-todo" </span><br><span class="line">          placeholder="What needs to be done?" </span><br><span class="line">          autoComplete="off"</span><br><span class="line">          type="text" </span><br><span class="line">          value=&#123;this.state.val&#125;//通过这样的方式，让value变得受控</span><br><span class="line">          onChange=&#123;(e)=&gt;&#123;</span><br><span class="line">            this.setState(&#123;</span><br><span class="line">              val:e.target.value</span><br><span class="line">            &#125;)</span><br><span class="line">          &#125;&#125;</span><br><span class="line">          onKeyDown=&#123;(e)=&gt;&#123;</span><br><span class="line">            let &#123;val&#125; = this.state</span><br><span class="line">            if(e.keyCode === 13)&#123;</span><br><span class="line">              if(!val.trim())&#123;</span><br><span class="line">                  alert("请输入内容");</span><br><span class="line">                  return ;</span><br><span class="line">              &#125;</span><br><span class="line">              addData(val); </span><br><span class="line">              this.setState(&#123;</span><br><span class="line">                val:""</span><br><span class="line">              &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">           // console.log(e.keyCode,val);</span><br><span class="line">          &#125;&#125;</span><br><span class="line"><span class="xml">        /&gt;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Create;</span><br></pre></td></tr></table></figure>



<h2 id="todos-js"><a href="#todos-js" class="headerlink" title="todos.js"></a>todos.js</h2><p>在这里，我们接受app中传递过来的数据，同时对于每一项todo都以每一列来单独进行处理，这里我们将其封装在li.js中，这样就只用在todos.js中将内容分别分发到li.js中即可。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React,&#123;PureComponent&#125; <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> Li <span class="keyword">from</span> <span class="string">'./li'</span>;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Todos</span> <span class="keyword">extends</span> <span class="title">PureComponent</span> </span>&#123;</span><br><span class="line">  render()&#123;</span><br><span class="line">    <span class="comment">//console.log(this.props.data);</span></span><br><span class="line">    <span class="comment">//console.log(11); </span></span><br><span class="line">    <span class="keyword">let</span> &#123;data&#125; = <span class="keyword">this</span>.props;</span><br><span class="line">    <span class="keyword">return</span> (<span class="xml"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"todo-list"</span>&gt;</span></span></span><br><span class="line"><span class="xml">            //这里需要注意的是，批量生成一定要有key值，这里我们使用数据的key属性</span></span><br><span class="line"><span class="xml">        &#123;data.map(item =&gt; <span class="tag">&lt;<span class="name">Li</span> <span class="attr">key</span>=<span class="string">&#123;item.id&#125;</span> &#123;<span class="attr">...this.props</span>&#125; <span class="attr">data</span>=<span class="string">&#123;item&#125;/</span>&gt;</span>)&#125;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Todos;</span><br></pre></td></tr></table></figure>

<h2 id="li-js"><a href="#li-js" class="headerlink" title="li.js"></a>li.js</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React,&#123;PureComponent,createRef&#125; <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Li</span> <span class="keyword">extends</span> <span class="title">PureComponent</span> </span>&#123;</span><br><span class="line">  state=&#123;</span><br><span class="line">      edit:<span class="literal">false</span>,</span><br><span class="line">      ref: createRef(),</span><br><span class="line">      val:<span class="string">""</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">static</span> getDerivedStateFromProps(props, state)&#123;</span><br><span class="line">    <span class="comment">//console.log(props,state); </span></span><br><span class="line">    <span class="comment">//console.log(props, state);</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        1. 挂载时：同步 state 中的val 和 props.txt</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="keyword">if</span>(!state.edit)&#123;</span><br><span class="line">        state.val = props.data.txt;</span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  componentDidUpdate(prevPorps,prevState)&#123;</span><br><span class="line">      <span class="comment">// 组件更新完成之后，判断是否需要给 input 获得焦点</span></span><br><span class="line">      <span class="keyword">if</span>(<span class="keyword">this</span>.state.edit&amp;&amp;!prevState.edit)&#123;</span><br><span class="line">            <span class="comment">// 让输入框获得焦点</span></span><br><span class="line">            <span class="comment">//console.log(this.refs);</span></span><br><span class="line">            <span class="comment">//this.refs.txt.select();</span></span><br><span class="line">            <span class="comment">//console.log();</span></span><br><span class="line">            <span class="keyword">this</span>.state.ref.current.select();</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;  </span><br><span class="line">  render()&#123;</span><br><span class="line">     <span class="keyword">let</span> &#123;data,changeDone,editTxt,remove&#125; = <span class="keyword">this</span>.props;</span><br><span class="line">     <span class="keyword">let</span> &#123;done,txt,id&#125; = data;</span><br><span class="line">     <span class="keyword">let</span> &#123;edit,val&#125; = <span class="keyword">this</span>.state; </span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;li className=&#123;edit?<span class="string">"editing"</span>:<span class="string">""</span>&#125;&gt;</span><br><span class="line">            &lt;div className=&#123;<span class="string">"todo "</span> + (done?<span class="string">"done"</span>:<span class="string">""</span>)&#125;&gt;</span><br><span class="line">                &lt;div className=<span class="string">"display"</span>&gt;</span><br><span class="line">                    &lt;input </span><br><span class="line">                        className=<span class="string">"check"</span> </span><br><span class="line">                        type=<span class="string">"checkbox"</span> </span><br><span class="line">                        checked=&#123;done&#125;</span><br><span class="line">                        onChange=&#123;(e)=&gt;&#123;</span><br><span class="line">                            <span class="built_in">console</span>.log(e.target.checked);</span><br><span class="line">                            changeDone(id,e.target.checked);</span><br><span class="line">                        &#125;&#125;</span><br><span class="line">                    /&gt;</span><br><span class="line">                    &lt;div </span><br><span class="line">                        className=<span class="string">"todo-content"</span></span><br><span class="line">                        onDoubleClick=&#123;()=&gt;&#123;</span><br><span class="line">                            <span class="keyword">this</span>.setState(&#123;</span><br><span class="line">                                edit:<span class="literal">true</span></span><br><span class="line">                            &#125;)</span><br><span class="line">                        &#125;&#125;</span><br><span class="line">                    &gt;&#123;txt&#125;&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">                    &lt;span </span></span><br><span class="line"><span class="regexp">                        className="todo-destroy"</span></span><br><span class="line"><span class="regexp">                        onClick=&#123;()=&gt;&#123;</span></span><br><span class="line"><span class="regexp">                            remove(id);</span></span><br><span class="line"><span class="regexp">                        &#125;&#125;</span></span><br><span class="line"><span class="regexp">                    &gt;&lt;/</span>span&gt;</span><br><span class="line">                &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">                &lt;div className="edit"&gt;</span></span><br><span class="line"><span class="regexp">                    &#123;/</span>* <span class="xml"><span class="tag">&lt;<span class="name">input</span> </span></span></span><br><span class="line"><span class="xml">                        ref="txt"</span></span><br><span class="line"><span class="xml">                        className="todo-input" </span></span><br><span class="line"><span class="xml">                        type="text" </span></span><br><span class="line"><span class="xml">                        value=&#123;txt&#125;</span></span><br><span class="line"><span class="xml">                        onChange=&#123;(e)=&gt;&#123;</span></span><br><span class="line"><span class="xml">                            editTxt(id,e.target.value)</span></span><br><span class="line"><span class="xml">                        &#125;&#125;</span></span><br><span class="line"><span class="xml">                        onBlur=&#123;()=&gt;&#123;</span></span><br><span class="line"><span class="xml">                            this.setState(&#123;</span></span><br><span class="line"><span class="xml">                                edit:false</span></span><br><span class="line"><span class="xml">                            &#125;)</span></span><br><span class="line"><span class="xml">                        &#125;&#125;</span></span><br><span class="line"><span class="xml">                    /&gt;</span> *<span class="regexp">/&#125;</span></span><br><span class="line"><span class="regexp">                    &lt;input </span></span><br><span class="line"><span class="regexp">                        ref=&#123;this.state.ref&#125;</span></span><br><span class="line"><span class="regexp">                        className="todo-input" </span></span><br><span class="line"><span class="regexp">                        type="text" </span></span><br><span class="line"><span class="regexp">                        value=&#123;val&#125;</span></span><br><span class="line"><span class="regexp">                        onChange=&#123;(e)=&gt;&#123;</span></span><br><span class="line"><span class="regexp">                           this.setState(&#123;</span></span><br><span class="line"><span class="regexp">                               val:e.target.value</span></span><br><span class="line"><span class="regexp">                           &#125;)</span></span><br><span class="line"><span class="regexp">                        &#125;&#125;</span></span><br><span class="line"><span class="regexp">                        onBlur=&#123;()=&gt;&#123;</span></span><br><span class="line"><span class="regexp">                            this.state.edit = false;</span></span><br><span class="line"><span class="regexp">                            if(val.trim())&#123;</span></span><br><span class="line"><span class="regexp">                                editTxt(id,val);</span></span><br><span class="line"><span class="regexp">                            &#125; else &#123;</span></span><br><span class="line"><span class="regexp">                                editTxt(id,txt);</span></span><br><span class="line"><span class="regexp">                            &#125;</span></span><br><span class="line"><span class="regexp">                        &#125;&#125;</span></span><br><span class="line"><span class="regexp">                    /</span>&gt;</span><br><span class="line">                &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">            &lt;/</span>div&gt;</span><br><span class="line">        &lt;<span class="regexp">/li&gt;</span></span><br><span class="line"><span class="regexp">        )</span></span><br><span class="line"><span class="regexp">  &#125;</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">export default Li;</span></span><br></pre></td></tr></table></figure>

<h2 id="state-js"><a href="#state-js" class="headerlink" title="state.js"></a>state.js</h2><p>主要是列表后面显示的待完成现象和已完成现象。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React,&#123;PureComponent&#125; <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">State</span> <span class="keyword">extends</span> <span class="title">PureComponent</span> </span>&#123;</span><br><span class="line">  render()&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;data,removeDone&#125; = <span class="keyword">this</span>.props;</span><br><span class="line">    <span class="keyword">let</span> doneData = data.filter(<span class="function"><span class="params">item</span>=&gt;</span>item.done);</span><br><span class="line">    <span class="keyword">let</span> unDoneData = data.filter(<span class="function"><span class="params">item</span>=&gt;</span>!item.done);</span><br><span class="line">    <span class="keyword">return</span> (<span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"todo-stats"</span>&gt;</span></span></span><br><span class="line">            &lt;span className="todo-count"&gt;</span><br><span class="line">              &lt;span className="number"&gt;&#123;unDoneData.length&#125;&lt;/span&gt; &lt;span className="word"&gt;项待完成&lt;/span&gt;</span><br><span class="line"><span class="xml">            <span class="tag">&lt;/<span class="name">span</span>&gt;</span>  </span></span><br><span class="line">            &#123;doneData.length&lt;1?"":&lt;span className="todo-clear"&gt;</span><br><span class="line">              &lt;a </span><br><span class="line">                href="#"</span><br><span class="line">                onClick=&#123;()=&gt;&#123;</span><br><span class="line">                  removeDone();</span><br><span class="line">                &#125;&#125;</span><br><span class="line">              &gt;</span><br><span class="line">                  Clear &lt;span&gt;&#123;doneData.length&#125;&lt;/span&gt; 已完成事项</span><br><span class="line">              &lt;/a&gt;</span><br><span class="line">            &lt;/span&gt;&#125;</span><br><span class="line">            </span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span>)</span></span><br><span class="line"><span class="xml">  &#125;</span></span><br><span class="line"><span class="xml">&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="xml">export default State;</span></span><br></pre></td></tr></table></figure>


    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/05/10/react/react%E5%88%9D%E5%AD%A6/" title="react初学">http://example.com/2021/05/10/react/react初学/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/react%E5%88%9D%E5%AD%A6/" rel="tag"># react初学</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/04/02/webpack/webpack%E5%8E%9F%E7%90%86/" rel="prev" title="webpack原理分析">
      <i class="fa fa-chevron-left"></i> webpack原理分析
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/05/14/react/react%20hooks/" rel="next" title="react hooks初学">
      react hooks初学 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#基本知识"><span class="nav-number">2.</span> <span class="nav-text">基本知识</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#setState"><span class="nav-number">2.1.</span> <span class="nav-text">setState</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用注意"><span class="nav-number">2.1.1.</span> <span class="nav-text">使用注意</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#props-与-state-的区别"><span class="nav-number">2.1.2.</span> <span class="nav-text">props 与 state 的区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#setState-的异步更新"><span class="nav-number">2.1.3.</span> <span class="nav-text">setState()的异步更新</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事件处理"><span class="nav-number">2.2.</span> <span class="nav-text">事件处理</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ToDoList实践"><span class="nav-number">3.</span> <span class="nav-text">ToDoList实践</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#APP-js"><span class="nav-number">3.1.</span> <span class="nav-text">APP.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#create-js"><span class="nav-number">3.2.</span> <span class="nav-text">create.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#todos-js"><span class="nav-number">3.3.</span> <span class="nav-text">todos.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#li-js"><span class="nav-number">3.4.</span> <span class="nav-text">li.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#state-js"><span class="nav-number">3.5.</span> <span class="nav-text">state.js</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

